//
//  DynamicColumnFieldCore+ExpandDynamicCellResponse.m
//  
//
//  Created by wjxfhxy on 15/12/30.
//
//

#import "DynamicColumnFieldCore+ExpandDynamicCellResponse.h"
#import "DynamicColumnFieldCore+Internal.h"
#import "DynamicColumnFieldCore+ChoicePhoto.h"
#import "DynamicColumnFieldCore+MoveViewController.h"
#import "ChoiceRecordViewController.h"
#import "ChoiceVoiceViewController.h"

@interface DynamicColumnFieldCore(ExpandDynamicCellResponseDelegate)<UITextFieldDelegate,MFMessageComposeViewControllerDelegate,TextInputGroupTableViewCellDelegate,TextAreaGroupTableViewCellDelegate,PositionGroupTableViewCellDelegate,DecimalGroupTableViewCellDelegate,ImagePanelGroupTableViewCellDelegate,ImagePanelStaticGroupTableViewCelldelegate,DropDownGroupTableViewCellDelegate>

@end

@implementation DynamicColumnFieldCore (ExpandDynamicCellResponse)

#pragma mark --
#pragma mark -- expandDynamicCell delegate

-(void)textInputGroupTableViewCell:(TextInputGroupTableViewCell *)cell textFieldDidBeginEditing:(UITextField *)textField
{
    _textInputViewCell = cell;
    
    if([_delegate respondsToSelector:@selector(dynamicColumnFieldCore:textFieldDidBeginEditingOfTextInputGroupTableViewCell:)])
       [_delegate dynamicColumnFieldCore:self textFieldDidBeginEditingOfTextInputGroupTableViewCell:cell];
}

- (void)textInputGroupTableViewCell:(TextInputGroupTableViewCell*)cell textFieldDidEndEditing:(UITextField *)textField
{
    _textInputViewCell = nil;
    
    if([_delegate respondsToSelector:@selector(dynamicColumnFieldCore:textFieldDidEndEditingOfTextInputGroupTableViewCell:)])
        [_delegate dynamicColumnFieldCore:self textFieldDidEndEditingOfTextInputGroupTableViewCell:cell];
}

-(BOOL)textInputGroupTableViewCell:(TextInputGroupTableViewCell *)cell textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)textAreaGroupTableViewCell:(TextAreaGroupTableViewCell *)cell textViewDidBeginEditing:(UITextView *)textView
{
    _textInputViewCell = cell;
}

-(void)textAreaGroupTableViewCell:(TextAreaGroupTableViewCell *)cell textViewDidEndEditing:(UITextView *)textView
{
    _textInputViewCell = nil;
}

-(BOOL)textInputGroupTableViewCell:(TextInputGroupTableViewCell *)cell textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    XMSQueueDictionary *constraint = [_dataSource constraintOfDynamicColumnFieldCore:self];
    
    [constraint enumerateArrayValueUsingBlock:^(id key, id obj, NSUInteger idx, BOOL *stop)
    {
        FieldDescript *fix = obj;
        if([fix.fieldName isEqualToString:cell.szTag] && fix.controlType.intValue == 8)
        {
            if(textField.text.length == 1 && [string isEqualToString:@""])
                ((PositionGroupTableViewCell*)cell).selectAddress = @"";
            *stop = YES;
        }
    }];
    return YES;
}

-(void)positionGroupTableViewCell:(PositionGroupTableViewCell *)cell didClick:(id)sender
{
    _selectedCell = cell;
    
    [UIViewController closeKeyBoard];
    
    XUIViewControllerSwitchParam * p = [XUIViewControllerSwitchParam new];
    
    //add\modify
    if(cell.text.length > 0)
    {
        NSString *selectAddress = cell.text;
        
        if(cell.selectAddress.length > 0)
            selectAddress = cell.selectAddress;
        
        NSArray *ar = [selectAddress componentsSeparatedByString:@";"];
        if(ar.count == 3)
            selectAddress = [NSString stringWithFormat:@"%@;%@;%@",ar[0],ar[1],cell.text];
        else
            selectAddress = [NSString stringWithFormat:@";;%@",cell.text];
        
        p.param[ChoiceCurrentPositionViewControllerDefaultPositionParamKey] = selectAddress;
    }
    //modify
    else if(cell.selectAddress.length > 0 && [cell.selectAddress isEqualToString:@";;"] == NO)
    {
        p.param[ChoiceCurrentPositionViewControllerDefaultPositionParamKey] = cell.selectAddress;
    }
    [XUINavigationController pushViewControllerClass:[ChoiceCurrentPositionViewController class] withSwitchParam:p];
}

-(void)imagePanelStaticGroupTableViewCell:(ImagePanelStaticGroupTableViewCell *)cell clickedImageButtonTag:(NSString *)tag imageContain:(NSArray *)imageContains
{
    NSMutableArray *arThumb = [NSMutableArray new];
    NSMutableArray *arOriURL = [NSMutableArray new];
    NSMutableArray *arContain = [NSMutableArray new];
    NSUInteger dIndex = 0;
    NSString *path = [[XMSFileHelper tempPath] stringByAppendingPathComponent:ImagePanelThumbDowdloadFolder];
    for(NSUInteger i = 0;i < cell.imageNames.count ;i++)
    {
        NSString *imageName = cell.imageNames[i];
        NSString *imagePath = [path stringByAppendingPathComponent:imageName];
        if([XMSFileHelper isFileExist:imagePath])
        {
            dIndex++;
            [arThumb addObject:imagePath];
            [arOriURL addObject:[XLGContext fileDownloadURL:imageName]];
            for(NSUInteger j = 0;j < imageContains.count ;j++)
            {
                id<UIViewExtendDelegate> d = imageContains[j];
                if([d.szTag rangeOfString:imageName].location != NSNotFound)
                {
                    [arContain addObject:d];
                }
                if([d.szTag isEqualToString:tag])
                    dIndex = j;
            }
        }
    }
    if(arThumb.count > 0)
    {
        ImageBrowserViewController *imgVcl = [[ImageBrowserViewController alloc]initWithThumbImagePath:arThumb originImageURL:arOriURL downloadSavePath:[XMSFileHelper tempPath]];
        [imgVcl defaultSelectAtIndex:dIndex];
        [imgVcl zoomPresentPreviewImageViewController:arContain];
    }
}

#pragma mark --
#pragma mark -- imagePanel delegate

- (void)clickAddImageButtonOfImagePanelGroupTableViewCell:(ImagePanelGroupTableViewCell *)cell
{
    _selectedCell = cell;
    
    [UIViewController closeKeyBoard];
    
    [self choicePhoto:3 - cell.selectedImagePath.count];
}

- (void)clickDeleteImageButtonOfImagePanelGroupTableViewCell:(ImagePanelGroupTableViewCell *)cell
{
    
}

- (void)clickDeleteModeImageButtonOfImagePanelGroupTableViewCell:(ImagePanelGroupTableViewCell *)cell atTag:(NSString *)tag
{
    
}


-(void)clickedClearButtonOfDropDownGroupTableViewCell:(DropDownGroupTableViewCell *)cell
{
    [_delegate dynamicColumnFieldCore:self didClickedClearButtonOfDropDownGroupTableViewCell:cell];
}

#pragma mark --
#pragma mark -- mixPanel delegate

-(void)mixedPanelStaticGroupTableViewCell:(MixedPanelStaticGroupTableViewCell *)cell clickedImageButtonTag:(NSString *)tag imageContain:(NSArray *)imageContains
{
    NSMutableArray *arThumb = [NSMutableArray new];
    NSMutableArray *arOriURL = [NSMutableArray new];
    NSMutableArray *arContain = [NSMutableArray new];
    NSUInteger dIndex = 0;
    NSString *path = [[XMSFileHelper tempPath] stringByAppendingPathComponent:ImagePanelThumbDowdloadFolder];
    for(NSUInteger i = 0;i < cell.imageNames.count ;i++)
    {
        NSString *imageName = cell.imageNames[i];
        NSString *imagePath = [path stringByAppendingPathComponent:imageName];
        if([XMSFileHelper isFileExist:imagePath])
        {
            dIndex++;
            [arThumb addObject:imagePath];
            [arOriURL addObject:[XLGContext fileDownloadURL:imageName]];
            for(NSUInteger j = 0;j < imageContains.count ;j++)
            {
                id<UIViewExtendDelegate> d = imageContains[j];
                if([d.szTag rangeOfString:imageName].location != NSNotFound)
                {
                    [arContain addObject:d];
                }
                if([d.szTag isEqualToString:tag])
                    dIndex = j;
            }
        }
    }
    if(arThumb.count > 0)
    {
        ImageBrowserViewController *imgVcl = [[ImageBrowserViewController alloc]initWithThumbImagePath:arThumb originImageURL:arOriURL downloadSavePath:[XMSFileHelper tempPath]];
        [imgVcl defaultSelectAtIndex:dIndex];
        [imgVcl zoomPresentPreviewImageViewController:arContain];
    }
}

- (void)clickAddButtonOfMixedPanelGroupTableViewCell:(MixedPanelGroupTableViewCell *)cell mixedTyp:(MixedPanelFunctionPanelType)mixedType
{
    _selectedCell = cell;
    
    [UIViewController closeKeyBoard];
    
    if(MixedPanelFunctionPanelTypePhotoAlbum == mixedType)
        [self choicePhotoWithMaxCount:3 sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    else if(MixedPanelFunctionPanelTypeTakePhoto == mixedType)
         [self choicePhotoWithMaxCount:3 sourceType:UIImagePickerControllerSourceTypeCamera];
    else if(MixedPanelFunctionPanelTypePosition == mixedType)
        [XUINavigationController pushViewControllerClass:[ChoiceCurrentPositionViewController class]];
    else if(MixedPanelFunctionPanelTypeVoice == mixedType)
        [XUINavigationController pushViewControllerClass:[ChoiceVoiceViewController class]];
    else if(MixedPanelFunctionPanelTypeRecord == mixedType)
        [XUINavigationController pushViewControllerClass:[ChoiceRecordViewController class]];
}

- (void)clickDeleteButtonOfMixedPanelGroupTableViewCell:(MixedPanelGroupTableViewCell *)cell mixedTyp:(MixedPanelFunctionPanelType)mixedType
{
    
}

- (void)clickDeleteModeButtonOfMixedPanelGroupTableViewCell:(MixedPanelGroupTableViewCell *)cell atTag:(NSString *)tag mixedTyp:(MixedPanelFunctionPanelType)mixedType
{

}

#pragma mark --
#pragma mark -- message delegate

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch (result)
    {
        case MessageComposeResultCancelled:
        {
            [UIViewController dismissViewControllerAnimated:YES completion:^{}];
            break;
        }
        case MessageComposeResultSent:
        {
            [XUIAlertView alertViewWithTitle:@"提示" message:@"短信发送成功" cancelButtonTitle:@"确定" otherButtonTitles:nil clickBlock:^(NSInteger nClickindex) {
                [UIViewController dismissViewControllerAnimated:YES completion:^{}];
            }];
            break;
        }
        case MessageComposeResultFailed:
        {
            [XUIAlertView alertViewWithTitle:@"提示" message:@"短信发送失败" cancelButtonTitle:@"确定" otherButtonTitles:nil clickBlock:^(NSInteger nClickindex) {
                [UIViewController dismissViewControllerAnimated:YES completion:^{}];
            }];
            break;
        }
        default:
        {
            [UIViewController dismissViewControllerAnimated:YES completion:^{}];
            break;
        }
    }
}

@end
